package com.lianjinsu.shop.repository.mapper.handler;

import com.alibaba.fastjson2.JSON;
import com.lianjinsu.shop.entity.Shop;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class GoodsListTypeHandler extends BaseTypeHandler<List<Shop>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<Shop> parameter, JdbcType jdbcType)
        throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
    }

    @Override
    public List<Shop> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return JSON.parseArray(rs.getString(columnName), Shop.class);
    }

    @Override
    public List<Shop> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return JSON.parseArray(rs.getString(columnIndex), Shop.class);
    }

    @Override
    public List<Shop> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return JSON.parseArray(cs.getString(columnIndex), Shop.class);
    }
}
